import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _InputBase from "@mui/material/InputBase";
import React, { memo, useEffect, useState } from "react";
import useSuperLock from "../../hooks/useSuperLock";
import toast from "../toast";
import { useTranslation } from "react-i18next";
import styles from "./verifictionCode.module.less";
import useServices from "../../hooks/useRequest";
// import { sendCode } from "@/api/user";
import { authCode } from "../../api/auth";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Component = function(props) {
    var target = props.target, _props_errMessage = props.errMessage, errMessage = _props_errMessage === void 0 ? "" : _props_errMessage, value = props.value, onChange = props.onChange, valid = props.canSendValid;
    var _useState = _sliced_to_array(useState(false), 2), show = _useState[0], setShow = _useState[1];
    var _useState1 = _sliced_to_array(useState(60), 2), second = _useState1[0], setSecond = _useState1[1];
    var t = useTranslation().t;
    var _useState2 = _sliced_to_array(useState(false), 2), hasError = _useState2[0], setError = _useState2[1];
    var targetValue = target.includes("-") ? target.split("-")[1] : target;
    var services = useServices({
        authCode: authCode
    });
    var _useSuperLock = _sliced_to_array(useSuperLock(/*#__PURE__*/ _async_to_generator(function() {
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    if ((targetValue === null || targetValue === void 0 ? void 0 : targetValue.trim().length) === 0 || hasError) {
                        return [
                            2
                        ];
                    }
                    return [
                        4,
                        services.authCode({
                            loginName: target
                        }).then(function(res) {
                            if ((res === null || res === void 0 ? void 0 : res.code) !== 200) {
                                toast.createToast({
                                    message: res.msg || t("sendCode0"),
                                    icon: "fail",
                                    type: "errToast"
                                });
                                return;
                            }
                            setShow(true);
                        }).catch(function() {})
                    ];
                case 1:
                    _state.sent();
                    return [
                        2
                    ];
            }
        });
    })), 1), getCode = _useSuperLock[0];
    useEffect(function() {
        if (valid) {
            var error = valid(target);
            if (error) {
                setError(true);
            } else {
                setError(false);
            }
        }
    }, [
        target
    ]);
    useEffect(function() {
        var timer = null;
        if (show) {
            timer = setInterval(function() {
                setSecond(function(pre) {
                    return pre - 1;
                });
            }, 1000);
        } else {
            setSecond(60);
        }
        if (second < 1) {
            setShow(false);
            clearInterval(timer);
        }
        return function() {
            clearInterval(timer);
        };
    }, [
        second,
        show
    ]);
    return /*#__PURE__*/ _jsxs("div", {
        className: styles.page,
        children: [
            /*#__PURE__*/ _jsx("div", {
                className: styles.code,
                children: /*#__PURE__*/ _jsx(_InputBase, {
                    placeholder: t("loginHeader7"),
                    className: styles.input,
                    value: value,
                    onChange: function(e) {
                        return onChange && onChange(e.target.value);
                    },
                    type: "tel"
                })
            }),
            show && second !== 0 ? /*#__PURE__*/ _jsx("div", {
                className: styles.sendDisable,
                children: "".concat(second, "s")
            }) : /*#__PURE__*/ _jsx("div", {
                className: (targetValue === null || targetValue === void 0 ? void 0 : targetValue.trim().length) === 0 || hasError ? styles.sendDisable : styles.send,
                onClick: function() {
                    return getCode();
                },
                children: t("loginHeader27")
            })
        ]
    });
};
var VerificationCode = /*#__PURE__*/ memo(Component);
export default VerificationCode;

 //# sourceMappingURL=index.js.map